package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * ProxySetGetOAD类
 * 实现代理设置后读取请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class ProxySetGetOAD implements AxdrType {

    private byte[] dataCode = null;
    private OAD soad = null;
    private Data sdata = null;
    private OAD road = null;
    private Unsigned8 delayTime = null;

    public ProxySetGetOAD() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(OAD soad, Data sdata, OAD road, Unsigned8 delayTime) {
        this.soad = soad;
        this.sdata = sdata;
        this.road = road;
        this.delayTime = delayTime;
    }

    public void setSoad(OAD soad) {
        this.soad = soad;
    }

    public void setSdata(Data sdata) {
        this.sdata = sdata;
    }

    public void setRoad(OAD road) {
        this.road = road;
    }

    public void setDelayTime(Unsigned8 delayTime) {
        this.delayTime = delayTime;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.soad = new OAD();
        codeLength += this.soad.decode(input);

        this.sdata = new Data();
        codeLength += this.sdata.decode(input);

        this.road = new OAD();
        codeLength += this.road.decode(input);

        this.delayTime = new Unsigned8();
        codeLength += this.delayTime.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.soad != null && this.sdata != null && this.road != null && this.delayTime != null) {
                codeLength = 0;
                codeLength += this.delayTime.encode(output);
                codeLength += this.road.encode(output);
                codeLength += this.sdata.encode(output);
                codeLength += this.soad.encode(output);
            } else {
                throw new RuntimeException("soad || sdata || road || delayTime is null");
            }
        }

        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding ProxySetGetOAD", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.soad != null && this.sdata != null && this.road != null && this.delayTime != null) {
            return "sequence: {" + "soad: " + this.soad + ", sdata: " + this.sdata + ", road: " + this.road
                    + ", delayTime: " + this.delayTime + "}";
        } else {
            return "soad || sdata || road || delayTime is null";
        }
    }

    public OAD getSoad() {
        return soad;
    }

    public Data getSdata() {
        return sdata;
    }

    public OAD getRoad() {
        return road;
    }

    public Unsigned8 getDelayTime() {
        return delayTime;
    }
}